define(["angularAMD", 'moment'], function (angularAMD, moment) {
    angularAMD.service('$CheckInService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog','$calculater',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog,$calculater){
            var service = this;

            //模具檢查
            function SMES_AccLifeCheck(machine_no, plotInfo, callback){
                var content = {
                    recordlist : [{
                        op_no : plotInfo.op_no,
                        plot_no: plotInfo.plot_no,
                        machine_no: machine_no,
                        qty : plotInfo.qty,
                    }
                ]};
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_ACC.acc_life_check_process',
                    content : content,
                    success : function(data){
                        if(data.life_manage == '0'){
                            //警示
                            $rootScope.showConfirm(data.description, function(confirmDialog){
                                confirmDialog.hide();
                                if(callback)
                                    callback();
                            });
                        } else if(data.life_manage == '1'){
                            //暫停生產
                            $rootScope.showAlert(data.description);
                        } else if(data.life_manage == '2'){
                            //核准
                            if(callback)
                                callback();
                        }
                    }
                });
            }

            //檢查始業點檢狀態
            function OpParameterInfo(machine_no, plotInfo, dialog, callback){
			    var content = {
                    machine_no: machine_no,
                    is_line : '0',
                    lot_no : plotInfo.plot_no,
                    op_no : plotInfo.op_no,
                    area_no : ''
                }   
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.start_in_op_parameter_info_get',
                    content : content,
                    success : function(data){
                        var errorMsg = '', warningMsg = '';
                        //生產線時(is_line=1)且需要始業檢查(isCheckSW = 1)時始業點檢須為全通過(isCheckEQP=2)
                        if(content.is_line == '1' && data.isCheckSW == 1 ){
                            if(data.start_check_result != 2){
                                //始業點檢尚未通過，不可進站
                                errorMsg +=$filter('translate')('mobile01.msg.starting_check_fail')+'<br>';
                            }
                        }
                        //需要點檢檢查(alert_mode  = 2)
                        if(data.alert_mode == 2 ){
                            errorMsg += data.alert_msg ;
                            dialog.hide();
                        } else if(data.alert_mode == 1 ){
                            warningMsg += data.alert_msg ;
                        }
                        
                        if(errorMsg != ''){
                            $rootScope.showAlert(errorMsg);
                        } else if(warningMsg != ''){
                            $rootScope.showConfirm(warningMsg, function(confirmDialog){
                                confirmDialog.hide();
                                if(callback){
                                    callback(data);
                                }
                            },function(confirmDialog){
                                confirmDialog.hide();
                                dialog.hide();
                            });
                        } else if(callback){
                            callback(data);
                        }
                    }
                });
            };

            //進站
            function SMES_PlotCheckInProcess(options, dialog){
                OpParameterInfo(options.machine_no, options.plotInfo, dialog, function(){
                    SMES_AccLifeCheck(options.machine_no, options.plotInfo, function(){
                        var content = {
                            plot_check_in :[{
                                //20220929 modify by wangzy for 0119887: 抛转ERP报工单发现，02ASF01-2208251606工单良品数大约工单生产数量
                                //report_datetime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                                 report_datetime: options.plotInfo.sysdate_time,
                                 machine_no:options.machine_no,
                                 plot_no:options.plotInfo.plot_no,
                                 op_seq:'',//
                                 op_no:options.plotInfo.op_no,
                                 workstation_no:'',//
                                 product_no : options.plotInfo.product_no,
                                 plot_stamp:options.plotInfo.plot_stamp,
                                 qty:options.plotInfo.qty,
                                 description:options.plotInfo.op_description
                            }]
                        };
                        $MMWService.sendToServer({
                            uri: 'ServicesSTD.Module_WIP.plot_check_in_process',
                            content : content,
                            success : function(data, response){
                                if( data.first_check_list && data.first_check_list.length > 0)
                                {
                                    var str = data.first_check_list.join("<br>");
                                    $rootScope.showAlert(str);
                                }
                                else
                                    $rootScope.showToast(response.Message);
                                if(options.callback)
                                    options.callback();
                            }
                        });
                    });
                });
            }

            service.init = function(options){
                /**
                 * options = {
                 *     machine_no : string, //機台代號
                 *     item : object, //plot資料
                 *     callback : function //確定後呼叫
                 * }
                **/
                (function openDialog(){
                    $mdDialog.dialog(
                        'module/moduleMobile/program/mobile01/CheckIn.tmp.html',
                        function (dialog) {
                            return {
                                item:options.item,
                                back:function(){
                                    dialog.hide();
                                },
                                //M#97342 進站數量開窗添加小數點
                                check_in_qty:function (event) 
                                {
                                    var target = event.currentTarget;
                                    $calculater.open(function(num,calcDialog)
                                    {
                                        dialog.item.qty = (+num).toFixed(4);
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        
                                    },function(calcDialog){
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum : dialog.item.qty,
                                        decimal : true
                                    });
                                },
                                confirm: function () {
                                    if(dialog.item.qty==0 || dialog.item.qty===undefined){
                                        $rootScope.showAlert($filter('translate')('mobile01.checkin.msg.qty_zero'),function(){
                                            dialog.item.qty = dialog.item.check_in_qty;
                                        });
                                    }else if((+dialog.item.check_in_qty) < (+dialog.item.qty)){
                                        $rootScope.showAlert($filter('translate')('mobile01.checkin.msg.greate_than_check_in_qty', dialog.item), function(){
                                            dialog.item.qty = dialog.item.check_in_qty;
                                        });
                                    }else{
                                        //回傳
                                        SMES_PlotCheckInProcess({
                                            machine_no: options.machine_no,
                                            plotInfo : dialog.item,
                                            callback : function(){
                                                dialog.hide();
                                                if(options.callback)
                                                    options.callback();
                                            }
                                        }, dialog);
                                    }
                                }
                            }
                        }
                    );
                })();
            };
        }
    ]);
});
